package com.jobchecks.sms.service;

import com.jobchecks.common.baseresult.JobchecksResult;
import com.jobchecks.dao.domain.SmsSendRecord;
import com.jobchecks.dao.mapper.SmsSendRecordMapper;
import com.jobchecks.sms.api.TestSmsService;
import com.jobchecks.sms.api.request.TestAddReq;
import com.jobchecks.sms.api.response.TestAddResp;
import com.jobchecks.sms.exception.SmsServiceException;
import com.jobchecks.sms.exception.SmsServiceExceptionCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.UUID;

/**
 * 测试服务实现
 *
 * @author huangjinjia
 */
@Service(version = "1.0.0", timeout = 30000, interfaceName = "testSmsService")
@Component
@Slf4j
public class TestSmsServiceImpl implements TestSmsService {

    @Autowired
    private SmsSendRecordMapper smsSendRecordMapper;

    @Override
    public JobchecksResult<TestAddResp> smsAdd(TestAddReq req){
        log.info("smsAdd接口开始");
        TestAddResp testResp = new TestAddResp();
        testResp.setId(UUID.randomUUID().toString());
        testResp.setNum(req.getAge());
        testResp.setCreateTime(new Date());
        testResp.setLevel(new BigDecimal(5));

        // 测试新增数据
        SmsSendRecord smsSendRecord = new SmsSendRecord();
        smsSendRecord.setId(UUID.randomUUID().toString().replace("-", ""));
        smsSendRecord.setCreateBy("hjj");
        smsSendRecord.setCreateTime(new Date());
        smsSendRecord.setUpdateBy("hjj2");
        smsSendRecord.setUpdateTime(new Date());
        smsSendRecord.setVersion(11L);
        smsSendRecord.setStatus(12);
        smsSendRecord.setUserId("userid0001");
        smsSendRecord.setSmsType(88);
        smsSendRecord.setSmsContent("XX任务没有执行成功，请关注" + req.getName());
        smsSendRecord.setSmsPhonenNumber("13800138000");
        smsSendRecord.setSmsReturnCode(req.getAge() + "");
        smsSendRecord.setSmsCode("231111");
        smsSendRecord.setSmsFunc("Interface");
        smsSendRecord.setSmsIp("127.0.0.1");
        // 入库
        smsSendRecordMapper.insertSelective(smsSendRecord);

        log.info("smsAdd接口结束");
        // 测试主动抛出异常
        if("test".equals(req.getName())){
            throw new SmsServiceException(SmsServiceExceptionCode.SMS_REQUEST_TIMEOUT);
        }
        return JobchecksResult.success(testResp);
    }

    @Override
    public JobchecksResult smsUpdate(String key, String conf) {
        log.info("smsUpdate接口开始");
        SmsSendRecord smsSendRecord = smsSendRecordMapper.selectByPrimaryKeySelective("d56141f4e2ba4f579bb3ad5809ba2208");
        log.info("查询结果：{}", smsSendRecord);

        int count = smsSendRecordMapper.countSelective(smsSendRecord);
        log.info("统计结果：{}", count);

        smsSendRecord.setUpdateBy("我是更新者");
        smsSendRecord.setUpdateTime(new Date());
        smsSendRecord.setSmsReturnCode(System.currentTimeMillis()+"");
        int updateCount = smsSendRecordMapper.updateByPrimaryKeySelective(smsSendRecord);
        log.info("更新结果：{}", updateCount);

        int deleteCount = smsSendRecordMapper.deleteByPrimaryKey("c5b269fa1ba64d81abccd0de1b3ede6a");
        log.info("删除结果：{}", deleteCount);

        SmsSendRecord querySmsSendRecord =  smsSendRecordMapper.selectBySelective(smsSendRecord);
        log.info("第二次查询结果：{}", querySmsSendRecord);

        log.info("smsUpdate接口结束");
        return JobchecksResult.success();
    }
}
